package com.jiusuzu.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jiusuzu.common.pojo.EUDataGridResult;
import com.jiusuzu.common.pojo.JiusuzuResult;
import com.jiusuzu.pojo.TbItem;
import com.jiusuzu.service.ItemService;

/**
 * 对商品增删改查
 * 
 * @author 李锐洋
 *
 */
@Controller
@RequestMapping("/item")
public class ItemController {
	@Autowired
	private ItemService itemService;

	/**
	 * 获取商品列表
	 * 
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/list")
	@ResponseBody
	public EUDataGridResult getItemList(Integer page, Integer rows) {
		EUDataGridResult result = itemService.getItemList(page, rows);
		return result;
	}   

	/**
	 * 保存新增商品
	 * 
	 * @param item
	 * @param desc
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save")
	@ResponseBody
	public JiusuzuResult saveItem(TbItem item, String desc, String itemParam) throws Exception {
		// 添加商品信息
		itemService.saveItem(item, desc, itemParam);
		return JiusuzuResult.ok();
	}

	/**
	 * 根据商品id获取商品对象
	 * 
	 * @param itemId
	 * @return
	 */
	@RequestMapping("/{itemId}")
	@ResponseBody
	public TbItem getItemById(@PathVariable Long itemId) {
		TbItem tbItem = itemService.getItemById(itemId);
		return tbItem;
	}

	/**
	 * 根据商品id下架该商品(可同时下架多个商品)
	 * 
	 * @param itemId
	 * @return
	 */
	@RequestMapping("/unshelve")
	@ResponseBody
	public JiusuzuResult ItemUnShelve(@RequestParam(value = "ids") String ids) {
		itemService.updateItemUnShelve(ids);
		return JiusuzuResult.ok();
	}

	/**
	 * 根据商品ids删除该商品(可同时下架删除多个商品)
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public JiusuzuResult ItemDelete(@RequestParam(value = "ids") String ids) {
		itemService.deleteItem(ids);
		return JiusuzuResult.ok();
	}

	/**
	 * 将已下架的商品重新上架（可同时上架多个商品）
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("/putaway")
	@ResponseBody
	public JiusuzuResult ItemPutaway(@RequestParam(value = "ids") String ids) {
		itemService.updateItemPutaway(ids);
		return JiusuzuResult.ok();
	}

	/**
	 * 编辑现有商品
	 * 
	 * @param item
	 * @return
	 */
	@RequestMapping("/update")
	@ResponseBody
	public JiusuzuResult ItemEdit(TbItem item, String desc, String itemParam) throws Exception {
		itemService.updateItemEdit(item, desc, itemParam);
		return JiusuzuResult.ok();
	}
}
